/**
 * Created by DELL on 2018/9/6.
 */
;(function () {
  window.onload = function () {
    game.init();
  }
  var game = window.game = {
    width: 0,
    height: 0,

    asset: null,
    stage: null,
    ticker: null,
    state: null,
    score: 0,

    bg: null,
    ground: null,
    bird: null,
    holdbacks: null,
    keyboard: null,  // 按键
    gameReadyScene: null,
    gameOverScene: null,

    init: function () {
      this.asset = new game.Asset();
      this.asset.on('complete', function (e) {
        this.asset.off('complete');
        this.initStage();
      }.bind(this));
      this.asset.load();
    },
    initStage: function () {
      this.width = Math.min(innerWidth, 450) * 2;
      this.height = Math.min(innerHeight, 750) * 2;
      this.scale = 0.5;

      //舞台画布
      var renderType = location.search.indexOf('dom') != -1 ? 'dom' : 'canvas';

      //舞台
      this.stage = new Hilo.Stage({
        renderType: renderType,
        width: this.width,
        height: this.height,
        scaleX: this.scale,
        scaleY: this.scale
      });
      document.body.appendChild(this.stage.canvas);

      //启动计时器
      this.ticker = new Hilo.Ticker(60);
      this.ticker.addTick(Hilo.Tween);
      this.ticker.addTick(this.stage);
      this.ticker.start(true);


      //绑定交互事件
      this.stage.enableDOMEvent(Hilo.event.POINTER_START, true);
      this.stage.on(Hilo.event.POINTER_START, this.onUserInput.bind(this));
      //舞台更新
      this.stage.onUpdate = this.onUpdate.bind(this);
      //初始化
      this.initBackground();

      this.initScenes();
      this.initCurrentScore();
      // this.initHoldbacks();
      // this.initKeyboard();

      // this.gameStart();
    },
    initScenes: function () {
      //准备场景
      this.gameReadyScene = new game.ReadyScene({
        id: 'readyScene',
        width: this.width,
        height: this.height,
        image: this.asset.ready
      }).addTo(this.stage);
    },
    onUserInput: function (e) {
      // if (this.state !== 'over' && !this.gameOverScene.contains(e.eventTarget)) {
      //启动游戏场景
      if (this.state !== 'playing') this.gameStart();
      //控制小鸟往上飞
      // this.bird.startFly();
      // }
    },
    onUpdate: function (delta) {
      // console.log(111);
      // if (this.state === 'ready') {
      //   return;
      // }
      // this.currentScore.setText(this.calcScore());
    },
    updataScore: function () {
      this.score = this.score + 1;
      this.currentScore.setText(this.score);
    },
    initBackground: function () {
      //背景
      var bgWidth = this.width * this.scale;
      var bgHeight = this.height * this.scale;

      var bgImg = this.asset.bg;
      this.bg = new Hilo.Bitmap({
        id: 'bg',
        image: bgImg,
        scaleX: this.width / bgImg.width,
        scaleY: this.height / bgImg.height
      }).addTo(this.stage);

      //地面
      var groundImg = this.asset.ground;
      var groundOffset = 60;
      this.ground = new Hilo.Bitmap({
        id: 'ground',
        image: groundImg,
        scaleX: (this.width + groundOffset * 2) / groundImg.width
      }).addTo(this.stage);

      //设置地面的y轴坐标
      this.ground.y = this.height - this.ground.height;

      //移动地面
      Hilo.Tween.to(this.ground, {
        x: -groundOffset * this.ground.scaleX
      }, {
        duration: 400,
        loop: true
      });
    },
    initCurrentScore: function () {
      //当前分数
      this.currentScore = new Hilo.BitmapText({
        id: 'score',
        glyphs: this.asset.numberGlyphs,
        textAlign: 'center'
      }).addTo(this.stage);

      //设置当前分数的位置
      this.currentScore.x = this.width - this.currentScore.width >> 1;
      this.currentScore.y = 180;
    },
    createKey: function () {
      var startX = Math.random() * (this.width - 40) + 20;
      new game.Key({
        id: 'key',
        atlas: this.asset.birdAtlas,
        height: this.height,
        startX: startX
      }).addTo(this.stage);
    },
    gameStart: function () {
      this.score = 0;
      this.state = 'playing';
      this.gameReadyScene.visible = false;
      this.currentScore.visible = true;
      this.currentScore.setText(this.score);
      this.createKey();
      setInterval(this.createKey.bind(this), 100);
    },
  }
})();